package action;

import Impl.adminDaoImpl;
import Impl.plateDaoImpl;
import entity.Plate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet(name = "PlateOpt")
public class PlateOpt extends HttpServlet {
    adminDaoImpl adminDao = new adminDaoImpl();
    plateDaoImpl plateDao = new plateDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("---------正在执行PlateOpt--------------");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        System.out.println(method);
        if (method.equals("show")){
            System.out.println("正在显示所有板块信息：");
            int pageNow = 0;
            if(request.getParameter("pageNow") == null) {
                pageNow = 1;
            }else {
                pageNow = Integer.parseInt(request.getParameter("pageNow"));
            }
            //       获取所有用户信息
            List<Plate> plates =plateDao.findPlateByPages(pageNow);
            List<Plate> plateList=plateDao.findAllPlate();
            int allcount = plateList.size();
            int allpage;
            if(allcount % 5 == 0){
                allpage=allcount / 5;
            }
            else{
                allpage=allcount / 5 + 1;
            }
            request.setAttribute("allcount",allcount);
            request.setAttribute("allpage", allpage);
            request.setAttribute("pageNow", pageNow);
            request.setAttribute("plateList",plates);
            request.getRequestDispatcher("plate-admin-list.jsp").forward(request,response);
        }else if (method.equals("add")){
            String sname = request.getParameter("sName");
            String masterName=request.getParameter("uName");
            System.out.println("正在新增板块："+sname);
            int type = Integer.parseInt(request.getParameter("sType"));
            if (type==1){
                type = Integer.parseInt(request.getParameter("select"));
            }
            System.out.println("父板块的ID："+type);
            int masterID = adminDao.findAdminByName(masterName).getId();
            Plate plate =new Plate(sname,masterID,type);
            plateDao.addPlate(plate);
        }else if (method.equals("edit")){
            int id = Integer.parseInt(request.getParameter("id"));
            String sname = request.getParameter("sName");
            System.out.println("正在修改板块："+sname);
            String masterName=request.getParameter("uName");
            int type = Integer.parseInt(request.getParameter("sType"));
            int masterID = adminDao.findAdminByName(masterName).getId();
            Plate plate =new Plate(id,sname,masterID,type);
            plateDao.updatePlate(plate);
        }else if (method.equals("del")){
            int id = Integer.parseInt(request.getParameter("id"));
            plateDao.deletePlateBysId(id);
        }else if (method.equals("search")){
            String condition = request.getParameter("condition");
            System.out.println("正在查找板块："+condition);
            List<Plate> plates = plateDao.findPlateByCondition(condition);
            int allcount=plates.size();
            request.setAttribute("page", "no");
            request.setAttribute("allcount",allcount);
            request.setAttribute("plateList",plates);
            request.getRequestDispatcher("plate-admin-list.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
